package com.example.note.service;

import com.example.note.mapper.NoteInfoMapper;
import com.example.note.mapper.UserInfoMapper;
import com.example.note.model.NoteInfo;
import com.example.note.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 */
@Service
public class UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private NoteInfoMapper noteInfoMapper;

    public UserInfo queryByName(String userName){
        return userInfoMapper.queryByName(userName);
    }

    public UserInfo queryById(Integer userId) {
        return userInfoMapper.queryById(userId);
    }

    public UserInfo getAuthorInfo(Integer noteId) {
        //如何通过笔记id获取作者的信息：先通过笔记id获取笔记信息，通过笔记信息，拿到笔记作者的id
        //，再通过作者的id，查询作者的信息
        NoteInfo noteInfo = noteInfoMapper.queryById(noteId);
        if(noteInfo == null || noteInfo.getUserId()<0){
            return null;
        }
        return  userInfoMapper.queryById(noteInfo.getUserId());
    }
}
